package timein8;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import org.testng.annotations.Test;

/**
 *
 *
 *
 * @author zhang peipei
 */
public class LocalDateTime1 {

    @Test
    public void ttt() {
        System.out.println(LocalDateTime.now());
    }


    /**
     * 从时间戳获得LocalDateTime
     */
    @Test
    public void t1() {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1662485248019L), ZoneId.systemDefault());
        int hour = localDateTime.getHour();
        System.out.println(localDateTime);

        System.out.println(hour);

        System.out.println(localDateTime.toLocalTime());

    }

    /**
     * 得到当前的时间
     */
    @Test
    private void t2() {


        LocalDateTime nowTime = LocalDateTime.now();
        System.out.println(nowTime);
        System.out.println(nowTime.getHour() + " : " + nowTime.getMinute() + " : " + nowTime.getSecond());
    }

    /**
     * 从秒数获得LocalDateTime
     */
    @Test
    private void t3() {

        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(1662485248L), ZoneId.systemDefault());
        System.out.println(localDateTime);
        // Asia/Shanghai
        System.out.println(ZoneId.systemDefault());
    }

    /**
     * 字符串得到一个LocalTime
     */
    @Test
    private void t4() {
        LocalTime localTime = LocalTime.parse("21:30:01");
        System.out.println(localTime);
        System.out.println(localTime.getHour() + " : " + localTime.getMinute() + " : " + localTime.getSecond());
    }

    /**
     * LocalTime比较大小
     * 调用方 早于 被比较者
     */
    @Test
    private void t5() {
        LocalTime localTime1 = LocalTime.parse("21:30:01");
        LocalTime localTime2 = LocalTime.parse("21:30:02");
        System.out.println(localTime1.compareTo(localTime2));
        System.out.println(System.currentTimeMillis());
    }

    @Test
    private void t6() {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1663050911000L), ZoneId.systemDefault());
        String s = localDateTime.toLocalDate().toString() + localDateTime.toLocalTime().toString();
        String replace = localDateTime.toString().replace('T', ' ');
        System.out.println(s);
        System.out.println(replace);
    }

    @Test
    private void t7() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
        String date = "20221013_050010";
        LocalDateTime from = LocalDateTime.from(formatter.parse(date));
        ZoneId zone = ZoneId.systemDefault();

        long timestamp = from.atZone(zone).toInstant().toEpochMilli();
        System.out.println(timestamp);

    }

    @Test
    private void t8() {
        DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
        String formattedDate = formatter.format(LocalDateTime.now());
        System.out.println(formattedDate);

        String format = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(LocalDateTime.now());
        System.out.println(format);
    }

    @Test
    private void t9() {
        OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneId.systemDefault());

        //2020-12-29T18:28:44+01:00
        var res = offsetDateTime.truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        System.out.println(res);

        OffsetDateTime offsetDateTime2 = OffsetDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault());
        var res2 = offsetDateTime2.truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        System.out.println(res2);

    }
}
